package com.system.stockservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.system.commonutils.bean.ApiResult;
import com.system.commonutils.pojo.Stock;
import com.system.commonutils.pojo.Stock_info;
import com.system.commonutils.utils.RedisUtil;
import com.system.stockservice.OtherClient;
import com.system.stockservice.bus.TestSender;
import com.system.stockservice.service.StockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: GPC
 * Date: 2020/07/07 8:51
 * Description:
 * Version: V1.0
 */
@RestController
@RefreshScope
@Api(value = "库存测试",tags = "库存服务测试API")
@DefaultProperties(defaultFallback = "golbalFallback")
@RequestMapping("stock")
@RabbitListener(queues = "orderserverIntegral")
public class StockController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    TestSender testSender;

    @Autowired
    StockService service;

    @Autowired(required = false)
    OtherClient otherClient;


//    @Autowired
//    MyProducer myProducer;


    /**
     *  处理消息队列中的订单
     *  根据订单来进行库存的减少
     * @param
     * @return
     */

    @RabbitHandler
    public void updateStockNmuber(String msg){
        System.out.println("订单号：orders_nou端口号：8080=================="+msg);
        if(msg!=null && !"".equals(msg)){
            System.out.println("updateStockNmuber");
        Object orderId1 = (String)redisUtil.get("orderId");
            System.out.println("updateStockNmuber"+orderId1);
        JSONObject jsonObject = JSONObject.parseObject(msg);
        String orderId = jsonObject.getString("orderId");
            System.out.println("updateStockNmuber");
        if(orderId1==null||!orderId.equals(orderId1)){
            redisUtil.set("orderId",orderId);
            System.out.println("消息后==="+orderId);
            ApiResult apiResult=service.updateStockNmuber(orderId);
        }else{
            System.out.println("错误========");
        }
        }
    }



//    @RequestMapping(value = "/goodsType", method = RequestMethod.POST,
//            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ApiOperation(value = "查询种类接口",notes = "查询商品类型",httpMethod = "post", response = ApiResult.class)
//    public ApiResult queryType(){
//
//        List<GoodsType> list = otherClient.goodsType();
//        if(list.size() > 0){
//            return ApiResult.successWithData("查询类型成功", list);
//        }else{
//            return ApiResult.failure("查询类型失败！");
//        }
//    }


    @ApiOperation(value = "查询库存商品")
    @PostMapping(value = "/select", produces = "application/json;charset=UTF-8")
    @HystrixCommand
    public ApiResult select(@RequestBody Stock stock){
        System.out.println(stock+"=查询库存商品select===========端口号：8080==========");
        ApiResult apiResult=service.selectStock(stock);
        return apiResult;
    }


    /**
     * 商品模块服务的外调接口
     * @param stock
     * @return
     */
    @ApiOperation(value = "通过商品类型id和品牌id查询库存商品")
    @PostMapping(value = "/selectByTBId", produces = "application/json;charset=UTF-8")
    @HystrixCommand
    public ApiResult selectByTBId(@RequestBody Stock stock){
        System.out.println("方法：selectByTBId==端口号：8080=========="+stock);
        List<Stock> result=service.selectByTBId(stock);
        List<Object>  list=new ArrayList<>();
        for(Stock s:result){
            List<Object>  list1=new ArrayList<>();
            list1.add(s.getStock_id());
            list1.add(s.getStock_name());
            list1.add(s.getStock_totalnumber());
            list.add(list1);
        }
        System.out.println(list.toString());
        ApiResult apiResult=new ApiResult();
        apiResult.setData(result);
        return apiResult;
    }



    /**
     *   修改库存里商品的信息
     * @param stock
     * @return
     */
    @ApiOperation(value = "修改库存商品")
    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    @HystrixCommand()
    public ApiResult update(@RequestBody Stock stock){
        System.out.println(stock+"controller=========update=端口号：8080============");
        ApiResult apiResult=service.updateStock(stock);
        return apiResult;
    }


    @ApiOperation(value = "添加库存商品")
    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @HystrixCommand
    public ApiResult add( @RequestBody Stock stock) throws Exception {
        System.out.println("add===========端口号：8080========="+stock);
        ApiResult apiResult=service.addStock(stock);
        return apiResult;
    }

    @ApiOperation(value = "查询库存商品详情表")
    @PostMapping(value = "/selectinfo", produces = "application/json;charset=UTF-8")
//  @HystrixCommand
    public ApiResult selectinfo(@RequestBody Stock_info stock){
        System.out.println("selectinfo==端口号：8080================"+stock);
        ApiResult apiResult=service.selectinfo(stock);
        return apiResult;
    }



    @ApiOperation(value = "修改库存商品详情表")
    @PostMapping(value = "/updateinfo", produces = "application/json;charset=UTF-8")
//  @HystrixCommand
    public ApiResult updateinfo(@RequestBody Stock_info stock){
        System.out.println(stock+"=======端口号：8080===========");
        ApiResult apiResult=service.updateinfo(stock);
        return apiResult;
    }



    /**
     * 进货接口
     * @param stock
     * @return
     */
    @ApiOperation(value = "进货")
    @PostMapping(value = "/addinfo", produces = "application/json;charset=UTF-8")
//  @HystrixCommand
    public ApiResult addinfo(@RequestBody Stock stock){
        System.out.println("add=========端口号：8080=========="+stock);
        ApiResult apiResult=service.addinfo(stock);
        return apiResult;
    }



    /**
     * 默认熔断回调
     * @return
     */
    public ApiResult golbalFallback(Throwable throwable){
//      String localizedMessage = throwable.getLocalizedMessage();
        return ApiResult.failure("系统出错请联系管理员");
    }

}
